using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Data;
using System.Windows.Media;

namespace Haidou.Wpf.Util.Converters;

public class StringToDoubleArray : IValueConverter
{
	public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
	{
		try
		{
			string? text = value as string;

			return string.IsNullOrWhiteSpace(text) ? new DoubleCollection { 1, 0 } : (object)DoubleCollection.Parse(Regex.Replace(text, " {2,}", " "));
		}
		catch (Exception)
		{
			return new DoubleCollection { 1, 0 };
		}
	}

	public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
	{
		DoubleCollection? col = value as DoubleCollection;

		return col?.ToString().Replace(',', ' ');
	}
}